package canss.webservice.controls;

import org.apache.beehive.controls.api.bean.Control;
import org.apache.beehive.controls.api.bean.ControlImplementation;

import weblogic.jdbc.SQLExceptionWrapper;
import canss.encoder.DBStringEncoder;
import canss.webservice.model.ContentsUsageCache;

import java.io.Serializable;

@ControlImplementation
public class ContentsUsageCacheControlImpl implements
		ContentsUsageCacheControl, Serializable
{
	private static final long serialVersionUID = -3182410113330612665L;

	@Control
	private BusinessSystemDB businesssystemDB;

	public Boolean InsertContentLinkByPID(String _tblPID_pid, String _tblCID_cid)
			throws SQLExceptionWrapper
	{
		int ret = businesssystemDB.InsertContentLinkByPID(DBStringEncoder.Server2DB(_tblPID_pid), DBStringEncoder.Server2DB(_tblCID_cid));
		return ret > 0;
	}

	public Boolean ResetEntireContentlinkByPID(String _tblPID_pid)
			throws SQLExceptionWrapper
	{
		int ret = businesssystemDB.ResetEntireContentlinkByPID(DBStringEncoder.Server2DB(_tblPID_pid));
		return ret > 0;
	}

	public String[] getContentsUsageByCID(String _tblCID_cid)
			throws SQLExceptionWrapper
	{
		ContentsUsageCache[] ret = businesssystemDB.getContentsUsageByCID(_tblCID_cid);
		for (ContentsUsageCache cuc : ret)
			ContentsUsageCacheControlImpl.DB2SERVER(cuc);
		String[] out = new String[ret.length];
		for (int i = 0; i < ret.length; i++)
			out[i] = ret[i].getTBLPID_PID();
		return out;
	}

	public String[] getcontentsUsageByPID(String _tblPID_pid)
			throws SQLExceptionWrapper
	{
		ContentsUsageCache[] ret = businesssystemDB.getContentsUsageByPID(_tblPID_pid);
		for (ContentsUsageCache cuc : ret)
			ContentsUsageCacheControlImpl.DB2SERVER(cuc);
		String[] out = new String[ret.length];
		for (int i = 0; i < ret.length; i++)
			out[i] = ret[i].getTBLCID_CID();
		return out;
	}

	private static final void DB2SERVER(ContentsUsageCache _cuc)
	{
		_cuc.setTBLCID_CID(DBStringEncoder.DB2Server(_cuc.getTBLCID_CID()));
		_cuc.setTBLPID_PID(DBStringEncoder.DB2Server(_cuc.getTBLPID_PID()));
	}
}